package com.zensation.ticklemepanda;

import android.graphics.Canvas;

public class GameLoopThread extends Thread {
	static final long FPS = 10;
	private MainGameView view;
	private boolean running = false;
	public static long currentTime = 0;
	public static long startTime = 0;
	private boolean isPause = false;
	private int timeLoop = 150;

	public GameLoopThread(MainGameView view) {
		this.view = view;
	}

	public void setRunning(boolean run) {
		running = run;
	}

	public boolean getRunning() {
		return running;
	}

	@Override
	public void run() {
		long ticksPS = 1000 / FPS;
		long startTime;

		long sleepTime;
		while (running) {
			if (!isPause) {
				Canvas c = null;
				startTime = System.currentTimeMillis();
				try {
					c = view.getHolder().lockCanvas();
					synchronized (view.getHolder()) {
						view.onDraw(c);
					}
				} finally {
					if (c != null) {
						view.getHolder().unlockCanvasAndPost(c);
					}
				}
				sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
				try {
					if (sleepTime > 40 && sleepTime < 60) {
						sleep(sleepTime);
					} else
						sleep(timeLoop);

				} catch (Exception e) {
				}
			}

		}
	}

	public boolean isPause() {
		return isPause;
	}

	public void setPause(boolean isPause) {
		this.isPause = isPause;
	}

	public int getTimeLoop() {
		return timeLoop;
	}

	public void setTimeLoop(int timeLoop) {
		this.timeLoop = timeLoop;
	}

}
